package com.elangzhi.ssm.modules.shop.controller;

import com.elangzhi.ssm.controller.AdminBaseController;
import com.elangzhi.ssm.controller.json.Tip;
import com.elangzhi.ssm.model.Account;
import com.elangzhi.ssm.model.AccountRole;
import com.elangzhi.ssm.model.Shop;
import com.elangzhi.ssm.model.User;
import com.elangzhi.ssm.modules.goods.services.GoodsService;
import com.elangzhi.ssm.modules.image.services.ImageService;
import com.elangzhi.ssm.modules.shop.services.ShopService;
import com.elangzhi.ssm.modules.user.services.UserService;
import com.elangzhi.ssm.services.AccountRoleService;
import com.elangzhi.ssm.services.AccountService;
import com.elangzhi.ssm.tools.UUIDFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
* 店铺 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/shop")
public class ShopController extends AdminBaseController<Shop> {

    /**
     * 允许开通店铺
     * @param shopId 店铺id
     * @return
     */
    @RequestMapping(value="/allowApply")
    @ResponseBody
    public Tip allowApply(Long shopId){
        Shop shop = shopService.selectById(shopId);
        shop.setStatus(1);
        try {
            shopService.updateById(shop);

            Account account = accountService.selectByTypeInfo(2,shop.getUserId());
            if(account != null){
                AccountRole accountRole = new AccountRole();
                accountRole.setId(UUIDFactory.getLongId());
                accountRole.setRoleId(5406064383888870462l);
                accountRole.setAccountId(account.getId());
                accountRoleService.insert(accountRole);
            }

            return new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("允许客户开通店铺失败");
            return new Tip(1);
        }
    }

    //---------------------------- property -------------------------------

    @Resource
    private ShopService shopService;

    @Resource
    private GoodsService goodsService;

    @Resource
    private ImageService imageService;

    @Resource
    private UserService userService;

    @Resource
    private AccountService accountService;

    @Resource
    private AccountRoleService accountRoleService;

}
